package br.domsd.pocketcontrol.action;

import java.io.File;
import java.util.Date;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import br.domsd.pocketcontrol.model.Period;

/**
 * 
 * @author Diego Martins
 * 
 */
public class XMLWriter
{

	private Document xmlDocument;
	private File file;
	
	public XMLWriter() {}
	
	public XMLWriter(File file, Document xmlOutput)
	{
		this.file = file;
		this.xmlDocument = xmlOutput;
	}
	
	public void setupXML(Period period)
	{
		Element rootElement = xmlDocument.createElement("pocketControl");
		xmlDocument.appendChild(rootElement);

		Element monthExpense = xmlDocument.createElement("monthExpenses");
		monthExpense.setAttribute("month", period.getMonth());
		monthExpense.setAttribute("year", period.getYear());
		rootElement.appendChild(monthExpense);

	}
	
	public void write()
	{
		try
		{
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer;
			transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(xmlDocument);

			StreamResult result = new StreamResult(file);
			transformer.transform(source, result);
		} 
		catch (TransformerConfigurationException e) 
		{
			e.printStackTrace();
		} 
		catch (TransformerException e)
		{
			e.printStackTrace();
		}

	}

	public void writeExpenseToXML(String date, String ammount)
	{
		xmlDocument.getDocumentElement().normalize();
		
		NodeList nList = xmlDocument.getElementsByTagName("monthExpenses");
		Node monthExpense = nList.item(0);
		
		if(date != null && ammount != null)
		{
				Element diaryExpense = xmlDocument.createElement("expense");
				diaryExpense.setAttribute("date", date.toString());
				diaryExpense.setAttribute("ammount", ammount);
				monthExpense.appendChild(diaryExpense);
		}
		
		DOMSource source = new DOMSource(xmlDocument);
		
		TransformerFactory transformerFactory = TransformerFactory
				.newInstance();
		Transformer transformer;
		try 
		{
			transformer = transformerFactory.newTransformer();
			StreamResult result = new StreamResult(file);
			transformer.transform(source, result);
		}
		catch (TransformerConfigurationException e) 
		{
			e.printStackTrace();
		}
		catch (TransformerException e)
		{
			e.printStackTrace();
		}

		

	}
	
	public Document getXmlOutput() 
	{
		return xmlDocument;
	}

	public void setXmlOutput(Document xmlOutput) 
	{
		this.xmlDocument = xmlOutput;
	}

	public void setFile(File file) 
	{
		this.file = file;
	}
	
	public File getFile()
	{
		return file;
	}

	
	/*
	 * 
	 * if(monthExpenses.getDiaryExpenses() != null && !monthExpenses.getDiaryExpenses().isEmpty())
		{
			for (Expense gasto : monthExpenses.getDiaryExpenses())
			{
				Element diaryExpense = xmlOutput.createElement("expense");
				diaryExpense.setAttribute("date", gasto.getData().toString());
				diaryExpense.setAttribute("ammount", gasto.getValor().toString());
				monthExpense.appendChild(diaryExpense);
			}
		}
	 * 
	 * 
	 */
	/*
	GastosMensais gastosMensais;
	Document xmlOutput;

	public XMLWriter(GastosMensais paramGastosMensais) throws ParserConfigurationException 
	{
		gastosMensais = paramGastosMensais;

		DocumentBuilderFactory docFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder docBuilder;
		docBuilder = docFactory.newDocumentBuilder();
		xmlOutput = docBuilder.newDocument();

	}

	public void prepareXML()
	{
		Element rootElement = xmlOutput.createElement("pocketControl");
		xmlOutput.appendChild(rootElement);

		Element gastoMensal = xmlOutput.createElement("gastosMensais");
		gastoMensal.setAttribute("mes", gastosMensais.getMes());
		gastoMensal.setAttribute("ano", gastosMensais.getAno());
		rootElement.appendChild(gastoMensal);

		for (Gasto gasto : gastosMensais.getGastosDiarios())
		{
			Element gastoDiario = xmlOutput.createElement("gasto");
			gastoDiario.setAttribute("data", gasto.getData().toString());
			gastoDiario.setAttribute("valor", gasto.getValor().toString());
			gastoMensal.appendChild(gastoDiario);
		}

	}

	public void write(String paramOutputFileName) {

		prepareXML();
		
		try
		{
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer;
			transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(xmlOutput);
			File file = new File(paramOutputFileName);
			System.out.println(file.getAbsolutePath());
			StreamResult result = new StreamResult(file);

			transformer.transform(source, result);

			System.out.println("File saved!");
		} 
		catch (TransformerConfigurationException e) 
		{
			e.printStackTrace();
		} 
		catch (TransformerException e)
		{
			e.printStackTrace();
		}

	}
	


	public void criarNovoXML(File file) {
		
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		
		Transformer transformer;
		try
		{
			transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(xmlOutput);
			StreamResult result = new StreamResult(file);
	
			transformer.transform(source, result);
	
			System.out.println("File saved!");
		}
		catch (TransformerConfigurationException e) 
		{
			e.printStackTrace();
		}
		catch (TransformerException e) 
		{
			e.printStackTrace();
		}


	}*/
}
